package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    //查询角色
    @Override
    public Object list(String name, String  label, Integer page, Integer pageSize) {
        //1.设置PageHelper参数分页
        PageHelper.startPage(page,pageSize);
        //2.调用Mapper方法进行查询
        List<Role> rolelist = roleMapper.selectList(name,label);
        //3.解析并封装结果
        Page<Role> p = (Page<Role>)rolelist;
        return new PageResult<>(p.getTotal(),p.getResult());

    }
    //添加角色
    @Override
    public void add(Role role) {
        //调用Mapper方法添加
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }
    //删除角色
    @Override
    public void deleteById(Integer id) {
        //调用Mapper方法删除
        roleMapper.deleteById(id);
    }
    //根据ID查询角色
    @Override
    public Role getById(Integer id) {
        //调用Mapper方法查询
        return roleMapper.selectById(id);
    }
    //修改角色
    @Override
    public void update(Role role) {
        //补全更新时间属性
        role.setUpdateTime(LocalDateTime.now());
        //调用Mapper方法更新
        roleMapper.updateById(role);
    }
    //查询所有角色
    @Override
    public List<Role> list() {
        // 查询所有角色，不进行分页
        return roleMapper.selectList(null, null);
    }

}
